package com.web.rss.gui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import com.sun.syndication.feed.synd.SyndEntryImpl;

public class FeedContentScrollPane extends JScrollPane {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2852891050422798978L;
	private HashMap<String, SyndEntryImpl> feedContent;
	public JList feedEntryList;
	private Vector<String> titlesList;
	
	public FeedContentScrollPane (HashMap<String, SyndEntryImpl> feedEntries) {
		feedContent = feedEntries;
		feedEntryList = new JList();
		this.updateContent(feedEntries);
	}
	
	public void updateContent(HashMap<String, SyndEntryImpl> feedEntries) {
		//feedContent = null;
		feedContent = feedEntries;
		titlesList = new Vector<String>();
		Set<String> feedTitles = feedEntries.keySet();
		Iterator<String> iter = feedTitles.iterator();
		while (iter.hasNext()) {
			titlesList.add(iter.next());
		}

		feedEntryList.setListData(titlesList);
		feedEntryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		feedEntryList.setSelectedIndex(0);

		feedEntryList.setVisible(true);
		this.setViewportView(feedEntryList);
	}
	
	public SyndEntryImpl getSelectedEntry() {
		SyndEntryImpl curr = new SyndEntryImpl();
		curr = feedContent.get(titlesList.get(feedEntryList.getSelectedIndex()));

		//System.out.println("Currently selected article: " + curr.getTitle());
		return curr;
	}
	
}
